home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility3 / wtj008.zip / FAULT.ZIP / FAULT.C next >
C/C++ Source or Header  |  1992-05-13  |  4KB  |  167 lines

  1. /*
  2.  * FAULT.C
  3.  */
  4.  
  5. #include <windows.h>
  6. #include <toolhelp.h>
  7. #include "fault.h"
  8.  
  9.  
  10. HANDLE      hInst;
  11. CATCHBUF    cb;                         //Stores register state.
  12. LPCATCHBUF  pCB=(LPCATCHBUF)&cb;        //Convenient pointer for the handler
  13. BOOL        fTrapGPFault;
  14.  
  15.  
  16. /*
  17.  * WinMain the Ubiquitous
  18.  */
  19.  
  20. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  21.                     LPSTR lpszCmdLine, int nCmdShow)
  22.     {
  23.     WNDCLASS        wc;
  24.     MSG             msg;
  25.     HWND            hWnd;
  26.  
  27.     hInst=hInstance;
  28.  
  29.     if (!hPrevInstance)
  30.         {
  31.         wc.style         = CS_HREDRAW | CS_VREDRAW;
  32.         wc.lpfnWndProc   = FaultWndProc;
  33.         wc.cbClsExtra    = 0;
  34.         wc.cbWndExtra    = 0;
  35.         wc.hInstance     = hInstance;
  36.         wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDR_ICON));
  37.         wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  38.         wc.hbrBackground = COLOR_APPWORKSPACE + 1;
  39.         wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU);
  40.         wc.lpszClassName = "Fault";
  41.  
  42.         if (!RegisterClass(&wc))
  43.             return FALSE;
  44.         }
  45.  
  46.     hWnd=CreateWindow("Fault", "GP Fault Handler",
  47.                       WS_MINIMIZEBOX | WS_OVERLAPPEDWINDOW,
  48.                       CW_USEDEFAULT, CW_USEDEFAULT, 400, 120,
  49.                       NULL, NULL, hInstance, NULL);
  50.  
  51.     if (NULL==hWnd)
  52.         return FALSE;
  53.  
  54.     ShowWindow(hWnd, nCmdShow);
  55.     UpdateWindow(hWnd);
  56.  
  57.     while (GetMessage(&msg, NULL, 0,0 ))
  58.         {
  59.         TranslateMessage(&msg);
  60.         DispatchMessage(&msg);
  61.         }
  62.  
  63.     return msg.wParam;
  64.     }
  65.  
  66.  
  67.  
  68.  
  69. /*
  70.  * FaultWndProc
  71.  */
  72.  
  73. long FAR PASCAL FaultWndProc(HWND hWnd, UINT iMsg, UINT wParam, LONG lParam)
  74.     {
  75.     HANDLE              hMem;
  76.     LPSTR               psz;
  77.     UINT                i;
  78.     static FARPROC      pfnInt=NULL;
  79.  
  80.     switch (iMsg)
  81.         {
  82.         case WM_CREATE:
  83.             pfnInt=MakeProcInstance((FARPROC)FaultHandler, hInst);
  84.  
  85.             if (NULL!=pfnInt)
  86.                 {
  87.                 //If we fail to register, fail the create and the application.
  88.                 if (!InterruptRegister(NULL, pfnInt))
  89.                     {
  90.                     MessageBox(hWnd, "InterruptRegister Failed.",
  91.                                "Fatal Error", MB_OK);
  92.                     return -1L;
  93.                     }
  94.                 }
  95.             break;
  96.  
  97.  
  98.         case WM_DESTROY:
  99.             if (NULL!=pfnInt)
  100.                 {
  101.                 InterruptUnRegister(NULL);
  102.                 FreeProcInstance(pfnInt);
  103.                 }
  104.  
  105.             PostQuitMessage(0);
  106.             break;
  107.  
  108.  
  109.         case WM_COMMAND:
  110.             switch (wParam)
  111.                 {
  112.                 case IDM_CAUSEGPFAULT:
  113.                     hMem=GlobalAlloc(GMEM_MOVEABLE, 512);
  114.                     psz=GlobalLock(hMem);
  115.  
  116.                     for (i=0; i<512; i++)    //Fill the memory with non-zeros
  117.                         psz[i]='X';
  118.  
  119.                     //Should GP Fault--there's no NULL terminator
  120.                     if (-1==SomeStrLen(psz))
  121.                         {
  122.                         MessageBox(hWnd, "SomeStrLen failed on GP Fault",
  123.                                    "Fault", MB_OK | MB_ICONEXCLAMATION);
  124.                         }
  125.                     else
  126.                         {
  127.                         //This should NOT happen.
  128.                         MessageBox(hWnd, "IMPOSSIBLE: Missed a GP Fault?!?",
  129.                                    "Fault", MB_OK | MB_ICONHAND);
  130.                         }
  131.  
  132.                     GlobalUnlock(hMem);
  133.                     GlobalFree(hMem);
  134.                     break;
  135.                 }
  136.             break;
  137.  
  138.  
  139.         default:
  140.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  141.         }
  142.  
  143.     return 0L;
  144.     }
  145.  
  146.  
  147.  
  148. /*
  149.  * SomeStrLen:  A bulletproof strlen.
  150.  */
  151.  
  152. UINT FAR PASCAL SomeStrLen(LPSTR psz)
  153.     {
  154.     UINT        cch=0;
  155.  
  156.     fTrapGPFault=TRUE;
  157.  
  158.     if (!Catch((LPCATCHBUF)&cb))
  159.         return -1;
  160.  
  161.     while (*psz++!=0)
  162.         cch++;
  163.  
  164.     fTrapGPFault=FALSE;
  165.     return cch;
  166.     }
  167.